<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Car extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'car';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'image_str',
        'images_array',
        'config_array',
        'attr_array',
        'tags_array',
        'shop_info',

    ];
    public function getTagsArrayAttr($value, $data)
    {
        return $data['tags'] ? explode(',',$data['tags']) : '';
    }
    public function getConfigArrayAttr($value, $data)
    {
        $arr= $data['config_json'] ? json_decode($data['config_json'], true) : '';
        if($arr){
            foreach($arr as &$v){
                $v['image']= $v['image'] ? cdnurl($v['image'], true) : '';
            }
        }
        return $arr;
    }
    public function getAttrArrayAttr($value, $data)
    {
        $arr = $data['attr_json'] ? json_decode($data['attr_json'], true) : '';
        $array = [];
        if($arr){
            foreach ($arr as $k => &$v) {
                $aa = ['title' => $k, 'value' => $v];
                array_push($array, $aa);
            }
        }
     
        return $array;
    }
    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }
    public function getImagesArrayAttr($value, $data)
    {
        $arr = explode(',', $data['images']);
        foreach ($arr as &$v) {
            $v = cdnurl($v, true);
        }
        return $arr;
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getshopInfoAttr($value, $data)
    {
        
        return Shop::get($data['shop_id']);
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function shop()
    {
        return $this->belongsTo('Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
